Ext.define('component.payDeposit.window',{
	extend : 'Ext.window.Window',
	callbackFun : function(record) {
		Ext.Msg.alert('提示', '回调函数未配置！');
	},
	config : {
		payDepositId : ''
	},
	initComponent : function() {
		var me = this;
		var payDepositId = this.payDepositId;
		var updateGridStore = Ext.create('Ext.data.Store', {
		    fields: ["memberName","documentType","documentNumber","mobile"],
		    //是否在服务端排序 （true的话，在客户端就不能排序）
		    remoteSort: false,
		    proxy: {
		        type: 'ajax',
		        url: contextPath+'/customer/familyMember/findList',
		        reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
		            type: 'json', //返回数据类型为json格式
		            root: 'obj'
		        },
		        actionMethods: {read: 'POST'},
	            extraParams:{
	            	buyId:payDepositId,
	            	memberType:'2'
	            }
		    },
		    autoLoad:true
		});
		
		
		
		var columns = [{
			text : "姓名",
			dataIndex : 'memberName',
			width:'20%',
			editor: {
                xtype: 'textfield'
            }
		}, {
			text : "证件类型",
			dataIndex : 'documentType',
			width:'20%',	
			editor: new Ext.form.field.ComboBox({
			    store: documentTypeStore,
			    queryMode: 'local',
			    displayField: 'name',
			    valueField: 'value'
            }),
            renderer: function(value,metadata,record){  
                var index = documentTypeStore.find('value',value);  
                if(index!=-1){  
                    return documentTypeStore.getAt(index).data.name;  
                }  
                return value;  
            }
		},{
			text : "证件号码",
			dataIndex : 'documentNumber',
			width:'20%',
			editor: {
                xtype: 'textfield'
            }
		},{
			text : "手机号码",
			dataIndex : 'mobile',
			width:'20%',
			editor: {
                xtype: 'textfield'
            }
		},{
        	text:'操作',
        	xtype : 'actioncolumn',
        	items:[{
        		 iconCls:'icon-delete',
        		 tooltip: '删除', 
        		 handler: function(grid, rowIndex, colIndex) {
        			 updateGridStore.removeAt(rowIndex);
        			  if(updateGridStore.getCount()<4){
   				    	Ext.getCmp("insertBtn").setVisible(true);
   				    }
        		 }
        	}]
        }];
		
		
		var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
	        clicksToEdit: 1
	    });
		
		var updateGridPanel=Ext.create('Ext.grid.Panel', {
			columnLines: true,
	        viewConfig: {
	            stripeRows: true
	        },
	    	plugins: [cellEditing],
			store : updateGridStore,
			columns : columns,
			tbar:[{text:'新增',iconCls:'icon-add',id:'insertBtn',
				handler:function(){
				    Ext.define('Record', { 
			            extend: 'Ext.data.Model' 
				    }); 
				    var record  = new Record(); 
				    updateGridStore.insert(0, record);
				    cellEditing.startEditByPosition({
				        row: 0, 
				    	column: 0
				    });
				    if(updateGridStore.getCount()>=4){
				    	Ext.getCmp("insertBtn").setVisible(false);
				    }
				    
			}
			},{
				text:'保存',iconCls:'icon-save',
				handler:function(){
					 var recordsCount=updateGridStore.getCount();
					 var jsonArray=[];
					 if(recordsCount>0){
						 for(var i=0;i<recordsCount;i++){
							 var documentNumber = updateGridStore.getAt(i).data.documentNumber;
	        				 if(documentNumber==null||documentNumber==""){
	        					 Ext.Msg.alert('提示', '请填写共同购房者证件号码!');
	        		           	 return;
	        				 }else{

	        					 //验证手机号码的正确性
	        					 var userPhone = updateGridStore.getAt(i).data.mobile;
	        					 if(userPhone!=null&&userPhone!=""){
	        						 var regTel = /^0?1[3|4|5|8][0-9]\d{8}$/;
	        						 if(!regTel.test(userPhone)){
	        							 Ext.Msg.alert('提示', '请输入正确的手机号码!');
	        							 return;
	        						 }
	        					 }
	        					 //验证证件号码正确性
	        					 var str = /(^\d{15}$)|(^\d{17}([0-9]|X)$)/;
	        					 if(str.test(documentNumber)==false){
	        						 Ext.Msg.alert('提示', '请输入共同购房者正确的证件号码!');
	        						 return;
	        					 }else{
	        						 jsonArray.push(Ext.JSON.encode(updateGridStore.getAt(i).data));
	        					 }
	        				 }
						 }
						 var result={};
     					result.familyMember=jsonArray;
     				 	var resultJson=Ext.JSON.encode(result);
     				 	 Ext.Ajax.request({
     		            	    url: contextPath+'/customer/payDeposit/payDepositChangeName',
     		            	    params: {
     		            	    	params: resultJson,
     		            	    	id:payDepositId
     		            	    },
     		            	    method: 'POST',
     		            	    success: function(response){
     		            	        var text = Ext.decode(response.responseText);
     		            	        if(text.res=="1"){
     		            	        	Ext.Msg.alert('提示', text.resMsg);
     		            	        	gridStore.load();
     		            	        	me.close();
     		            	        }
     		            	       
     		            	    }
     		            	});
					 }else{
						 Ext.Msg.alert('提示', '请添加共同购房者!');
					 }
				}
			}
			]
		});
		
		
	
		
		var mainPanel=Ext.create('Ext.panel.Panel', {
		    items:[updateGridPanel]
		});
		
		var defaultConfig = {
				frame:true,
				title : '更名',
				width: 700,
			    height: 500,
			    x: 10,
			    y: 200,
			    plain: true,
		        modal: true,
				layout : 'fit',
				items:[mainPanel]
			};
			Ext.apply(this,defaultConfig);
			this.callParent();
	}
	
	
	
});